﻿using KPI.Api.Apis;
using KPI.Entity;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using WPF_TY_UI;

namespace KPIClient.Views
{
    /// <summary>
    /// ViewQuestionList.xaml 的交互逻辑
    /// </summary>
    public partial class ViewQuestionList : TYUserControl
    {
        ViewQuestionListModel vm = new ViewQuestionListModel();
        public ViewQuestionList()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += ViewQuestionList_Loaded;
        }

        private void ViewQuestionList_Loaded(object sender, RoutedEventArgs e)
        {
            BtnClick_Search(null, null);
        }

        private async void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            pager.PageIndex = 1;
            await QueryData();
        }

        private void BtnClick_Create(object sender, RoutedEventArgs e)
        {
            ViewCreateQuestion view = new ViewCreateQuestion();
            view.ShowDialog();
        }

        private async void Pager_IndexChanged(object sender, RoutedEventArgs e)
        {
            //去查询数据
            await QueryData();
        }

        async Task<bool> QueryData()
        {
            QuestionBankQueryDto query = new QuestionBankQueryDto();
            query.page = pager.PageIndex;
            query.limit = pager.PageSize;

            query.questionName = vm.Name;


            if (vm.SelectedQtType != null && vm.SelectedQtType.Key >= 0)
            {
                query.queryType = vm.SelectedQtType.Key;
            }
            if (vm.DtStart != null)
            {
                query.startDate = vm.DtStart;
            }
            if (vm.DtEnd != null)
            {
                query.endDate = vm.DtEnd;
            }

            vm.ListQuestionBank.Clear();
            var data = await Run(() => QuestionBankApi.GetQuestionBankList(query));
            if (data == null || data.code != 0)
            {
                return false;
            }
            foreach (var item in data.data?.list)
            {
                QuestionInfoModel model = new QuestionInfoModel();
                model.FromData(item);
                model.Index = vm.ListQuestionBank.Count + 1;
                vm.ListQuestionBank.Add(model);
            }
            pager.DataCount = data.data.total;
            pager.PageCount = data.data.pages;
            return true;
        }

        private void BtnClick_Preview(object sender, RoutedEventArgs e)
        {
            //ViewPreviewQuestion 
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx != null)
            {
                ViewPreviewQuestion view = new ViewPreviewQuestion(dx.Data.qbId);
                view.ShowDialog();
            }
        }

        private async void BtnClick_Delete(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionInfoModel;
            if (dx != null)
            {
                if (MessageBox.Show($"是否确认删除 :{dx.Title}?", "提示", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }
                var qbId = dx.Data.qbId;
                {
                    var rData = await Run(() => QuestionBankApi.DeleteQuestionBank(qbId));
                    if (rData == null)
                    {
                        MessageBox.Show("数据异常,请检查日志!");
                        return;
                    }
                    if (rData.code != 0)
                    {
                        MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                        return;
                    }
                }
                await QueryData();
            }
        }
    }
}
